package com.study.samsung.Utils;

import cn.hutool.core.util.PageUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * page工具类
 */
public class Pager implements Serializable {
    private Integer pageNo;//当前页码
    private Integer pageSize;//每页的数量
    private Integer total;//总条数9
    private List<?> data;//当前的数据

    public boolean getHasPrev(){
        return !(pageNo==1);
    }

    public boolean getHasNext(){
        return !(pageNo==getPageCount());
    }

    /**
     * 计算的总页数
     * @return
     */
    public Integer getPageCount(){
        if(total%pageSize==0){
            return total/pageSize;
        }else{
            return total/pageSize+1;
        }
    }

    public List<Integer> getRainbow(){
        int[] rainbow = PageUtil.rainbow(pageNo, getPageCount(), 5);
        ArrayList<Integer> objects = new ArrayList<>();
        for (int i = 0; i < rainbow.length; i++) {
            objects.add(rainbow[i]);
        }
        return objects;
    }



    public Pager(Integer no, Integer size){
        this.pageNo=no;
        this.pageSize=size;
    }




    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public List<?> getData() {
        return data;
    }

    public String getCaiHong(){
        StringBuilder stringBuilder=new StringBuilder();
        if (getPageCount()==1){
            return "";
        }
        if (getPageCount()>5){
            if (pageNo>=5&pageNo<=getPageCount()-3){
                stringBuilder.append("<span onclick='fenye("+(pageNo-1)+")'><</span> <span onclick='fenye("+(1)+")'>1</span>");
                stringBuilder.append("<span onclick='fenye("+(pageNo-5==0?1:pageNo-5)+")'>...</span>");
                stringBuilder.append("<span onclick='fenye("+(pageNo-2)+")'>"+(pageNo-2)+"</span>");
                stringBuilder.append("<span onclick='fenye("+(pageNo-1)+")'>"+(pageNo-1)+"</span>");
                stringBuilder.append("<span class='active' onclick='fenye("+(pageNo)+")'>"+(pageNo)+"</span>");
                stringBuilder.append("<span onclick='fenye("+(pageNo+1)+")'>"+(pageNo+1)+"</span>");
                stringBuilder.append("<span onclick='fenye("+(pageNo+2)+")'>"+(pageNo+2)+"</span>");
                stringBuilder.append("<span onclick='fenye("+(pageNo+5>getPageCount()?getPageCount():pageNo+5)+")'>...</span>");
                stringBuilder.append("<span onclick='fenye("+(getPageCount())+")'>"+(getPageCount())+"</span>"+"<span "+(pageNo==getPageCount()?"":"onclick='fenye("+(pageNo+1)+")'")+" >></span>");
            }else if(pageNo<5){
                stringBuilder.append("<span class='"+(pageNo==1?"no-allow":"")+"' "+(pageNo==1?"":"onclick='fenye("+(pageNo-1)+")'")+"><</span> <span class='"+(pageNo==1?"active":"")+"' "+(pageNo==1?"":"onclick='fenye("+(1)+")'")+">1</span>");
                stringBuilder.append("<span class='"+(pageNo==2?"active":"")+"' onclick='fenye("+2+")'>"+2+"</span>");
                stringBuilder.append("<span class='"+(pageNo==3?"active":"")+"' onclick='fenye("+3+")'>"+3+"</span>");
                stringBuilder.append("<span class='"+(pageNo==4?"active":"")+"' onclick='fenye("+4+")'>"+4+"</span>");
                stringBuilder.append("<span class='"+(pageNo==5?"active":"")+"' onclick='fenye("+5+")'>"+5+"</span>");
                stringBuilder.append("<span class='"+(pageNo==6?"active":"")+"' onclick='fenye("+6+")'>"+6+"</span>");
                stringBuilder.append("<span onclick='fenye("+(pageNo+5>getPageCount()?getPageCount():pageNo+5)+")'>...</span>"+"<span onclick='fenye("+(getPageCount())+")'>"+(getPageCount())+"</span>"+"<span class='"+(pageNo==getPageCount()?"no-allow":"" )+"' "+(pageNo==getPageCount()?"":"onclick='fenye("+(pageNo+1)+")'")+">></span>");
            }else if (pageNo>=getPageCount()-3){
                stringBuilder.append("<span onclick='fenye("+(pageNo-1)+")'><</span> <span onclick='fenye("+(1)+")'>1</span>");
                stringBuilder.append("<span onclick='fenye("+(pageNo-5)+")'>...</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-5)?"active":"")+"' onclick='fenye("+(getPageCount()-5)+")'>"+(getPageCount()-5)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-4)?"active":"")+"' onclick='fenye("+(getPageCount()-4)+")'>"+(getPageCount()-4)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-3)?"active":"")+"' onclick='fenye("+(getPageCount()-3)+")'>"+(getPageCount()-3)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-2)?"active":"")+"' onclick='fenye("+(getPageCount()-2)+")'>"+(getPageCount()-2)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-1)?"active":"")+"' onclick='fenye("+(getPageCount()-1)+")'>"+(getPageCount()-1)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==getPageCount()?"active":"")+"' onclick='fenye("+(getPageCount())+")'>"+(getPageCount())+"</span>");
                stringBuilder.append("<span class='"+(pageNo==getPageCount()?"no-allow":"")+"' "+(pageNo==getPageCount()?"":"onclick='fenye("+(pageNo+1)+")'")+">></span>");
            }
        }else {
            stringBuilder.append("<span class='"+(pageNo==1?"no-allow":"")+"'"+(pageNo==1?"":"onclick='fenye("+(pageNo-1)+")'")+" >"+"<"+"</span>");
            for (int i=1;i<=getPageCount();i++){
                stringBuilder.append("<span class='"+(pageNo==i?"active":"")+"' onclick='fenye("+i+")'>"+i+"</span>");
            }
            stringBuilder.append("<span class='"+(pageNo==getPageCount()?"no-allow":"")+"' "+(pageNo==getPageCount()?"":"onclick='fenye("+(pageNo+1)+")'")+" >"+">"+"</span>");
        }
        return stringBuilder.toString();
    }


    public String getFlcaihong(){
        StringBuilder stringBuilder=new StringBuilder();
        if (getPageCount()==1){
            return "";
        }
        if (getPageCount()>5){
            if (pageNo>=5&pageNo<=getPageCount()-3){
                stringBuilder.append("<span onclick='flquery("+(pageNo-1)+")'><</span> <span onclick='flquery("+(1)+")'>1</span>");
                stringBuilder.append("<span onclick='flquery("+(pageNo-5==0?1:pageNo-5)+")'>...</span>");
                stringBuilder.append("<span onclick='flquery("+(pageNo-2)+")'>"+(pageNo-2)+"</span>");
                stringBuilder.append("<span onclick='flquery("+(pageNo-1)+")'>"+(pageNo-1)+"</span>");
                stringBuilder.append("<span class='active' onclick='flquery("+(pageNo)+")'>"+(pageNo)+"</span>");
                stringBuilder.append("<span onclick='flquery("+(pageNo+1)+")'>"+(pageNo+1)+"</span>");
                stringBuilder.append("<span onclick='flquery("+(pageNo+2)+")'>"+(pageNo+2)+"</span>");
                stringBuilder.append("<span onclick='flquery("+(pageNo+5>getPageCount()?getPageCount():pageNo+5)+")'>...</span>");
                stringBuilder.append("<span onclick='flquery("+(getPageCount())+")'>"+(getPageCount())+"</span>"+"<span "+(pageNo==getPageCount()?"":"onclick='flquery("+(pageNo+1)+")'")+" >></span>");
            }else if(pageNo<5){
                stringBuilder.append("<span class='"+(pageNo==1?"no-allow":"")+"' "+(pageNo==1?"":"onclick='flquery("+(pageNo-1)+")'")+"><</span> <span class='"+(pageNo==1?"active":"")+"' "+(pageNo==1?"":"onclick='flquery("+(1)+")'")+">1</span>");
                stringBuilder.append("<span class='"+(pageNo==2?"active":"")+"' onclick='flquery("+2+")'>"+2+"</span>");
                stringBuilder.append("<span class='"+(pageNo==3?"active":"")+"' onclick='flquery("+3+")'>"+3+"</span>");
                stringBuilder.append("<span class='"+(pageNo==4?"active":"")+"' onclick='flquery("+4+")'>"+4+"</span>");
                stringBuilder.append("<span class='"+(pageNo==5?"active":"")+"' onclick='flquery("+5+")'>"+5+"</span>");
                stringBuilder.append("<span class='"+(pageNo==6?"active":"")+"' onclick='flquery("+6+")'>"+6+"</span>");
                stringBuilder.append("<span onclick='flquery("+(pageNo+5>getPageCount()?getPageCount():pageNo+5)+")'>...</span>"+"<span onclick='flquery("+(getPageCount())+")'>"+(getPageCount())+"</span>"+"<span class='"+(pageNo==getPageCount()?"no-allow":"" )+"' "+(pageNo==getPageCount()?"":"onclick='flquery("+(pageNo+1)+")'")+">></span>");
            }else if (pageNo>=getPageCount()-3){
                stringBuilder.append("<span onclick='flquery("+(pageNo-1)+")'><</span> <span onclick='flquery("+(1)+")'>1</span>");
                stringBuilder.append("<span onclick='flquery("+(pageNo-5)+")'>...</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-5)?"active":"")+"' onclick='flquery("+(getPageCount()-5)+")'>"+(getPageCount()-5)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-4)?"active":"")+"' onclick='flquery("+(getPageCount()-4)+")'>"+(getPageCount()-4)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-3)?"active":"")+"' onclick='flquery("+(getPageCount()-3)+")'>"+(getPageCount()-3)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-2)?"active":"")+"' onclick='flquery("+(getPageCount()-2)+")'>"+(getPageCount()-2)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-1)?"active":"")+"' onclick='flquery("+(getPageCount()-1)+")'>"+(getPageCount()-1)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==getPageCount()?"active":"")+"' onclick='flquery("+(getPageCount())+")'>"+(getPageCount())+"</span>");
                stringBuilder.append("<span class='"+(pageNo==getPageCount()?"no-allow":"")+"' "+(pageNo==getPageCount()?"":"onclick='flquery("+(pageNo+1)+")'")+">></span>");
            }
        }else {
            stringBuilder.append("<span class='"+(pageNo==1?"no-allow":"")+"'"+(pageNo==1?"":"onclick='flquery("+(pageNo-1)+")'")+" >"+"<"+"</span>");
            for (int i=1;i<=getPageCount();i++){
                stringBuilder.append("<span class='"+(pageNo==i?"active":"")+"' onclick='flquery("+i+")'>"+i+"</span>");
            }
            stringBuilder.append("<span class='"+(pageNo==getPageCount()?"no-allow":"")+"' "+(pageNo==getPageCount()?"":"onclick='flquery("+(pageNo+1)+")'")+" >"+">"+"</span>");
        }
        return stringBuilder.toString();
    }


    public String getSxcaihong(){
        StringBuilder stringBuilder=new StringBuilder();
        if (getPageCount()==1){
            return "";
        }
        if (getPageCount()>5){
            if (pageNo>=5&pageNo<=getPageCount()-3){
                stringBuilder.append("<span onclick='sxquery("+(pageNo-1)+")'><</span> <span onclick='sxquery("+(1)+")'>1</span>");
                stringBuilder.append("<span onclick='sxquery("+(pageNo-5==0?1:pageNo-5)+")'>...</span>");
                stringBuilder.append("<span onclick='sxquery("+(pageNo-2)+")'>"+(pageNo-2)+"</span>");
                stringBuilder.append("<span onclick='sxquery("+(pageNo-1)+")'>"+(pageNo-1)+"</span>");
                stringBuilder.append("<span class='active' onclick='sxquery("+(pageNo)+")'>"+(pageNo)+"</span>");
                stringBuilder.append("<span onclick='sxquery("+(pageNo+1)+")'>"+(pageNo+1)+"</span>");
                stringBuilder.append("<span onclick='sxquery("+(pageNo+2)+")'>"+(pageNo+2)+"</span>");
                stringBuilder.append("<span onclick='sxquery("+(pageNo+5>getPageCount()?getPageCount():pageNo+5)+")'>...</span>");
                stringBuilder.append("<span onclick='sxquery("+(getPageCount())+")'>"+(getPageCount())+"</span>"+"<span "+(pageNo==getPageCount()?"":"onclick='sxquery("+(pageNo+1)+")'")+" >></span>");
            }else if(pageNo<5){
                stringBuilder.append("<span class='"+(pageNo==1?"no-allow":"")+"' "+(pageNo==1?"":"onclick='sxquery("+(pageNo-1)+")'")+"><</span> <span class='"+(pageNo==1?"active":"")+"' "+(pageNo==1?"":"onclick='sxquery("+(1)+")'")+">1</span>");
                stringBuilder.append("<span class='"+(pageNo==2?"active":"")+"' onclick='sxquery("+2+")'>"+2+"</span>");
                stringBuilder.append("<span class='"+(pageNo==3?"active":"")+"' onclick='sxquery("+3+")'>"+3+"</span>");
                stringBuilder.append("<span class='"+(pageNo==4?"active":"")+"' onclick='sxquery("+4+")'>"+4+"</span>");
                stringBuilder.append("<span class='"+(pageNo==5?"active":"")+"' onclick='sxquery("+5+")'>"+5+"</span>");
                stringBuilder.append("<span class='"+(pageNo==6?"active":"")+"' onclick='sxquery("+6+")'>"+6+"</span>");
                stringBuilder.append("<span onclick='sxquery("+(pageNo+5>getPageCount()?getPageCount():pageNo+5)+")'>...</span>"+"<span onclick='sxquery("+(getPageCount())+")'>"+(getPageCount())+"</span>"+"<span class='"+(pageNo==getPageCount()?"no-allow":"" )+"' "+(pageNo==getPageCount()?"":"onclick='sxquery("+(pageNo+1)+")'")+">></span>");
            }else if (pageNo>=getPageCount()-3){
                stringBuilder.append("<span onclick='sxquery("+(pageNo-1)+")'><</span> <span onclick='sxquery("+(1)+")'>1</span>");
                stringBuilder.append("<span onclick='sxquery("+(pageNo-5)+")'>...</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-5)?"active":"")+"' onclick='sxquery("+(getPageCount()-5)+")'>"+(getPageCount()-5)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-4)?"active":"")+"' onclick='sxquery("+(getPageCount()-4)+")'>"+(getPageCount()-4)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-3)?"active":"")+"' onclick='sxquery("+(getPageCount()-3)+")'>"+(getPageCount()-3)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-2)?"active":"")+"' onclick='sxquery("+(getPageCount()-2)+")'>"+(getPageCount()-2)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-1)?"active":"")+"' onclick='sxquery("+(getPageCount()-1)+")'>"+(getPageCount()-1)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==getPageCount()?"active":"")+"' onclick='sxquery("+(getPageCount())+")'>"+(getPageCount())+"</span>");
                stringBuilder.append("<span class='"+(pageNo==getPageCount()?"no-allow":"")+"' "+(pageNo==getPageCount()?"":"onclick='sxquery("+(pageNo+1)+")'")+">></span>");
            }
        }else {
            stringBuilder.append("<span class='"+(pageNo==1?"no-allow":"")+"'"+(pageNo==1?"":"onclick='sxquery("+(pageNo-1)+")'")+" >"+"<"+"</span>");
            for (int i=1;i<=getPageCount();i++){
                stringBuilder.append("<span class='"+(pageNo==i?"active":"")+"' onclick='sxquery("+i+")'>"+i+"</span>");
            }
            stringBuilder.append("<span class='"+(pageNo==getPageCount()?"no-allow":"")+"' "+(pageNo==getPageCount()?"":"onclick='sxquery("+(pageNo+1)+")'")+" >"+">"+"</span>");
        }
        return stringBuilder.toString();
    }

    public String getCollectfy(){
        StringBuilder stringBuilder=new StringBuilder();
        if (getPageCount()==1){
            return "";
        }
        if (getPageCount()>5){
            if (pageNo>=5&pageNo<=getPageCount()-3){
                stringBuilder.append("<span onclick='collectionfy("+(pageNo-1)+")'><</span> <span onclick='collectionfy("+(1)+")'>1</span>");
                stringBuilder.append("<span onclick='collectionfy("+(pageNo-5==0?1:pageNo-5)+")'>...</span>");
                stringBuilder.append("<span onclick='collectionfy("+(pageNo-2)+")'>"+(pageNo-2)+"</span>");
                stringBuilder.append("<span onclick='collectionfy("+(pageNo-1)+")'>"+(pageNo-1)+"</span>");
                stringBuilder.append("<span class='active' onclick='collectionfy("+(pageNo)+")'>"+(pageNo)+"</span>");
                stringBuilder.append("<span onclick='collectionfy("+(pageNo+1)+")'>"+(pageNo+1)+"</span>");
                stringBuilder.append("<span onclick='collectionfy("+(pageNo+2)+")'>"+(pageNo+2)+"</span>");
                stringBuilder.append("<span onclick='collectionfy("+(pageNo+5>getPageCount()?getPageCount():pageNo+5)+")'>...</span>");
                stringBuilder.append("<span onclick='collectionfy("+(getPageCount())+")'>"+(getPageCount())+"</span>"+"<span "+(pageNo==getPageCount()?"":"onclick='collectionfy("+(pageNo+1)+")'")+" >></span>");
            }else if(pageNo<5){
                stringBuilder.append("<span class='"+(pageNo==1?"no-allow":"")+"' "+(pageNo==1?"":"onclick='collectionfy("+(pageNo-1)+")'")+"><</span> <span class='"+(pageNo==1?"active":"")+"' "+(pageNo==1?"":"onclick='collectionfy("+(1)+")'")+">1</span>");
                stringBuilder.append("<span class='"+(pageNo==2?"active":"")+"' onclick='collectionfy("+2+")'>"+2+"</span>");
                stringBuilder.append("<span class='"+(pageNo==3?"active":"")+"' onclick='collectionfy("+3+")'>"+3+"</span>");
                stringBuilder.append("<span class='"+(pageNo==4?"active":"")+"' onclick='collectionfy("+4+")'>"+4+"</span>");
                stringBuilder.append("<span class='"+(pageNo==5?"active":"")+"' onclick='collectionfy("+5+")'>"+5+"</span>");
                stringBuilder.append("<span class='"+(pageNo==6?"active":"")+"' onclick='collectionfy("+6+")'>"+6+"</span>");
                stringBuilder.append("<span onclick='collectionfy("+(pageNo+5>getPageCount()?getPageCount():pageNo+5)+")'>...</span>"+"<span onclick='collectionfy("+(getPageCount())+")'>"+(getPageCount())+"</span>"+"<span class='"+(pageNo==getPageCount()?"no-allow":"" )+"' "+(pageNo==getPageCount()?"":"onclick='collectionfy("+(pageNo+1)+")'")+">></span>");
            }else if (pageNo>=getPageCount()-3){
                stringBuilder.append("<span onclick='collectionfy("+(pageNo-1)+")'><</span> <span onclick='collectionfy("+(1)+")'>1</span>");
                stringBuilder.append("<span onclick='collectionfy("+(pageNo-5)+")'>...</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-5)?"active":"")+"' onclick='collectionfy("+(getPageCount()-5)+")'>"+(getPageCount()-5)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-4)?"active":"")+"' onclick='collectionfy("+(getPageCount()-4)+")'>"+(getPageCount()-4)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-3)?"active":"")+"' onclick='collectionfy("+(getPageCount()-3)+")'>"+(getPageCount()-3)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-2)?"active":"")+"' onclick='collectionfy("+(getPageCount()-2)+")'>"+(getPageCount()-2)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==(getPageCount()-1)?"active":"")+"' onclick='collectionfy("+(getPageCount()-1)+")'>"+(getPageCount()-1)+"</span>");
                stringBuilder.append("<span class='"+(pageNo==getPageCount()?"active":"")+"' onclick='collectionfy("+(getPageCount())+")'>"+(getPageCount())+"</span>");
                stringBuilder.append("<span class='"+(pageNo==getPageCount()?"no-allow":"")+"' "+(pageNo==getPageCount()?"":"onclick='collectionfy("+(pageNo+1)+")'")+">></span>");
            }
        }else {
            stringBuilder.append("<span class='"+(pageNo==1?"no-allow":"")+"'"+(pageNo==1?"":"onclick='collectionfy("+(pageNo-1)+")'")+" >"+"<"+"</span>");
            for (int i=1;i<=getPageCount();i++){
                stringBuilder.append("<span class='"+(pageNo==i?"active":"")+"' onclick='collectionfy("+i+")'>"+i+"</span>");
            }
            stringBuilder.append("<span class='"+(pageNo==getPageCount()?"no-allow":"")+"' "+(pageNo==getPageCount()?"":"onclick='collectionfy("+(pageNo+1)+")'")+" >"+">"+"</span>");
        }
        return stringBuilder.toString();
    }
    public void setData(List<?> data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Pager{" +
                "pageNo=" + pageNo +
                ", pageSize=" + pageSize +
                ", total=" + total +
                ", data=" + data +
                '}';
    }
}
